import organizationManagementApi from '@/biz/organizationManagement/organizationManagement'
import toolApi from '@/biz/base/tool'

function returnRender() {
  return (h, {
    data
  }) => {
    return h(
      'span', {
        style: {
          display: 'inline-block',
          width: '100%',
        },
      },
      [
        h('span', [
          h('Icon', {
            props: {
              type: data.parentId=='0'&&data.children.length>0?'ios-folder-outline':'ios-paper-outline',
            },
            style: {
              marginRight: '8px',
            },
          }),
          h('span', data.name),
        ]),
      ]
    )
  }
}
//循环遍历部门
function forEachDepartment(mess, name) {
  mess.map((item) => {
    item.title = item[name]
    item.children && forEachDepartment(item.children, name)
    item.expand = item.parentId=='0'?true:false
    item.buildingNum = item.buildingNum ? item.buildingNum : 0
    item.render = returnRender()
    // item.children = item.parentId=='0'? item.children : []
  })
}
//获取部门列表
function getDepartmentList(data) {
  return organizationManagementApi.getOA(data).then((res) => {
    if (res) {
      res[0].selected = true
      forEachDepartment(res, 'name')
      return res
    }
  })
}
//循环遍历部门
function forEachOA(mess, name) {
  mess.map((item) => {
    item.title = item[name]
    item.expand = item.level == 1?true:false
    item.children && forEachOA(item.children, name)
  })
}
//获取业务组织架构
function getBusinessList() {
  return toolApi.organization().then((res) => {
    if (res) {
      res[0].selected = true
      forEachOA(res, 'orgName')
      console.log('部门列表treeListBusiness', res)
      return res
    }
  })
}
export default {
  getDepartmentList,
  getBusinessList
}